﻿using HK.Core.Common;
using HK.Core.Load.Protocol.Data;
using HK.Core.Load.Protocol.Signal;
using HK.Core.Utils;
using UnityEngine;

namespace HK.Core.Load.Data
{
    
    /// <summary>
    /// Entity对象
    /// </summary>
    public abstract class EntityInfo : AssetBase, IEntityInfo
    {
        
        /// <summary>
        /// 实例Id
        /// </summary>
        public int InstanceId { get; protected set; } = -1;

        /// <summary>
        /// AssetKey
        /// </summary>
        public string AssetKey => Key;

        /// <summary>
        /// 加载资产类型
        /// </summary>
        public override LoadAssetType LoadType => LoadAssetType.Entity;
        
        /// <summary>
        /// 资产类型
        /// </summary>
        public AssetFileType AssetType { get; private set; } = AssetFileType.Unknown;

        /// <summary>
        /// 设定信息
        /// </summary>
        /// <param name="iAssetKey">AssetKey</param>
        /// <param name="iOptions">选项</param>
        /// <param name="iAsync">异步标志位</param>
        /// <param name="iCoolTime">倒计时冷却时间(默认 : 30秒)</param>
        public override void SetInfo(string iAssetKey, int iOptions, bool iAsync = false, float iCoolTime = Const.DefaultCoolTime)
        {
            // 设定信息
            base.SetInfo(iAssetKey, iOptions, iAsync, iCoolTime);
            
            AssetType = UtilsIo.GetAssetFileTypeByAssetKey(iAssetKey, out var oExtension);
        }

        /// <summary>
        /// 清空
        /// </summary>
        public override void Clear()
        {
            base.Clear();

            InstanceId = -1;
            AssetType = AssetFileType.Unknown;
        }
    }

    /// <summary>
    /// Entity对象
    /// </summary>
    /// <typeparam name="TEntity">Entity对象类型</typeparam>
    public sealed class EntityInfo<TEntity> : EntityInfo, IEntityInfo<TEntity>
        where TEntity : UnityEngine.Object
    {

#region Entity

        /// <summary>
        /// 实例化对象
        /// </summary>
        public TEntity Entity { get; private set; } = default(TEntity);

        /// <summary>
        /// 设定Entity
        /// </summary>
        /// <param name="iEntity">Entity</param>
        /// <param name="iInstanceId">实例Id</param>
        /// <param name="iFrom">加载来源</param>
        public void SetEntity(TEntity iEntity, int iInstanceId, LoadFrom iFrom)
        {
            Entity = iEntity;
            InstanceId = iInstanceId;
            From = iFrom;
        }

#endregion        

        /// <summary>
        /// 清空
        /// </summary>
        public override void Clear()
        {
            base.Clear();

            InstanceId = -1;
            Entity = default(TEntity);
        }
        
        /// <summary>
        /// 释放自身
        /// </summary>
        /// <returns>true:释放成功; false:释放失败;</returns>
        protected override bool ReleaseSelf()
        {
            if (default(TEntity) != Entity)
            {
                Object.Destroy(Entity);
                Entity = default(TEntity);
                return true;
            }
            return false;
        }
        
    }
    
}